/*
 * @Author: yuelei 
 * @Date: 2018-05-10 21:39:38 
 * @Last Modified by: Yuelei
 * @Last Modified time: 2018-05-13 16:50:08
 */
import {query,add,remove} from '../services/class'

export default {
  namespace: 'class',

  state: {
    classes: [],
  },

  effects: {
    //   获取商品
    *query(_, { call, put }) {
      const response = yield call(query);
      console.log('class')
      console.log(response)
      console.log('class')
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *remove({ payload: params }, { call, put }) {
   
      const response = yield call(remove, params);
      yield put({
        type: 'reduce',
        payload: response,
      });
    },
    *add({ payload: params }, { call, put }) {
      const response = yield call(add, params);
      yield put({
        type: 'extend',
        payload: response,
      });
    },
  },

  reducers: {
    //   操作reducer
    save(state, action) {
      console.log('请求成功')
 
      return {
        ...state,
        
        classes: action.payload,
      };
    },
    extend(state, { payload: obj }) {
      const { classes } = state;
      return {
        ...state,
        classes: [obj, ...classes],
      };
    },
    reduce(state, { payload: obj }) {
      const { classes } = state;
      return {
        ...state,
        classes: [...classes],
      };
    },
  },
  subscriptions: {
    setup({ history, dispatch }) {
      dispatch({ type: 'query' });
    },
  },
};
